package www.admin.utils;

import com.google.common.base.Strings;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Result {

    private Integer code;

    private String msg;

    private Object data;

    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData("");
        return result;
    }

    public static Result success(Object data) {
        return success().data(data);
    }

    public static Result failure(Integer code, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setData(data);
        return result;
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(ResultCode resultCode, String msg) {
        return failure(resultCode).msg(msg);
    }

    public static Result success(ResultCode resultCode, String msg) {
        return failure(resultCode).msg(msg);
    }

    public Result code(Integer code) {
        this.code = code;
        return this;
    }

    public Result msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result data(Object data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return Strings.nullToEmpty(msg);
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResultCode.SUCCESS.code();
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.msg();
    }
}
